package com.carryGo.web.dp;

import com.carryGo.entity.Business;
import com.carryGo.entity.Commodity;
import com.carryGo.entity.ShoppCart;
import com.carryGo.entity.User;
import com.carryGo.service.dp.CommodityService;
import com.carryGo.service.dp.UserService;
import com.carryGo.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * ClassName:Purchase
 * package:guoban
 * Desription:
 * 购买跳转
 * @Date:2020/8/28 20:38
 * @Author: 邓攀
 */
@WebServlet("/purchase")
@MultipartConfig
public class Purchase extends HttpServlet {

    private UserService userService = new UserService();
    private CommodityService commodityService = new CommodityService();
    private HttpServletRequest request;
    private HttpServletResponse response;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.request = request;
        this.response = response;
        String method = request.getParameter("method");

        switch (method) {
            case "toPay": toPay(); break;
            case "toShoppCart": toShoppCart(); break;
            case "delShoppCart": delShoppCart(); break;
            case "toAlipayTradePagePay": toAlipayTradePagePay(); break;
        }
    }

    private void toAlipayTradePagePay() throws ServletException, IOException {
        WebUtils.toRequestPage(request,response,"alipay.trade.page.pay");
    }


    /**
     * 删除购物车数据
     */
    private void delShoppCart() throws ServletException, IOException {
        String str = request.getParameter("id");
        String[] split = str.split(",");
        int id = Integer.parseInt(split[0]);
        String userName = split[1];
        ShoppCart shoppCart = commodityService.queryShoppCart(id);
        del(shoppCart);
        List<ShoppCart> shoppCarts = commodityService.queryShoppCart(userName);
        request.setAttribute("shoppCarts",shoppCarts);
        WebUtils.toRequestPage(request,response,"shoppCart");
    }

    /**
     * 直接购买
     */
    private void toPay() throws ServletException, IOException {
        String str = request.getParameter("id");
        String[] split = str.split(",");
        int id = Integer.parseInt(split[0]);
        String userName = split[1];
        int number = Integer.parseInt(split[2]);
        ShoppCart shoppCart = commodityService.queryShoppCart(id);
        String reference = UUID.randomUUID().toString().replaceAll("\\-","");
        String commName = shoppCart.getCommName();
        double sum = shoppCart.getPrice() * number;
        User user = userService.queryUserByUsername(userName);
        String address = user.getAddress();
        Business business = new Business(reference,commName,userName,number,sum,address);
        request.setAttribute("business",business);
        WebUtils.toRequestPage(request,response,"pay");
    }

    /**
     * 加入购物车
     */
    private void toShoppCart() throws ServletException, IOException {
        String str = request.getParameter("id");
        System.out.println(str);
        String[] split = str.split(",");
        int id = Integer.parseInt(split[0]);
        System.out.println(id);
        String userName = split[1];
        System.out.println(userName);
        int number =Integer.parseInt(split[2]);
        System.out.println(number);
        Commodity commodity = commodityService.queryCommodity(id);
        ShoppCart shoppCart = new ShoppCart(commodity.getCommName(),commodity.getBusinessName(),userName,number,commodity.getPrice(),number*commodity.getPrice(),commodity.getPic());
        addCommodity(shoppCart);
        List<ShoppCart> shoppCarts = commodityService.queryShoppCart(userName);
        request.setAttribute("shoppCarts",shoppCarts);
        WebUtils.toRequestPage(request,response,"shoppCart");
    }

    /**
     * 增加购物车数据
     */
    public boolean addCommodity(ShoppCart shoppCart) {
        try {
            return userService.insert(shoppCart) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 删除数据
     */
    public int del(ShoppCart shoppCart) {
        try {
            return userService.delete(shoppCart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
